home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1720 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  869 b 

  1. Path: unix.sri.com!usenet
  2. From: mklenk@updike.sri.com (Mark Klenk)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: realloc->free?
  5. Date: 16 Jan 1996 16:05:27 GMT
  6. Organization: Nuance Communications
  7. Message-ID: <4dgic7$qin@unix.sri.com>
  8. References: <4df2ud$706@oxy.rust.net>
  9. Reply-To: mklenk@updike.sri.com
  10. NNTP-Posting-Host: 204.75.161.40
  11.  
  12.  
  13. Earl Bennett wrote:
  14. >
  15. >realloc() will free the old block.  It is perfectly legal to say
  16. >
  17. >  a = realloc(a, newsize);
  18. >
  19. >No memory loss should occur from this.
  20.  
  21.     Excuse me???  What about if realloc fails?!
  22.     Then you've lost at least the old number of bytes,
  23.     because it returns NULL in that case, overwriting
  24.     the pointer 'a', whose old value is now lost.
  25.  
  26.     You should ALWAYS do this instead:
  27.  
  28.         b = realloc(a, newsize);
  29.         if (b == NULL) {
  30.             /* At least we haven't lost 'a'. */
  31.         }
  32.  
  33.  
  34. ---
  35.  
  36. mklenk@coronacorp.com       (Mark Klenk)
  37.  
  38.  
  39.  
  40.